import Vue from 'vue'
import vueRouter from 'vue-router'
import store from '../vuex/store'
Vue.use(vueRouter)


/*登录*/
import login from '@/pages/login.vue'
/*后台*/
import context from '@/pages/context.vue'
/*首页*/
import index from '@/pages/index.vue'
/*图书*/
import book from '@/pages/book.vue'
import booklist from '@/pages/booklist.vue'
import bookAdd from '@/pages/bookAdd.vue'
import bookEdit from '@/pages/bookEdit.vue'
import bookdetail from '@/pages/bookdetail.vue'
/*设置*/
import setting from '@/pages/setting.vue'
import userInfo from '@/pages/userInfo.vue'
import password from '@/pages/password.vue'

const routes=[
   { path: '/', redirect: '/login'}, 
   { path: '/login',  name:'login', component: login, title:'登录'},
   { path: '/context', name:'context', component: context, 
      children: [
        {
          path: '/',
          redirect: 'index',
          show:false
        },
        {
          path: 'index',
          component: index,
          title: "首页",
          show:true,
          icon: 'fa-indent'
        },
        {
          path: 'book',
          component: book,
          title: "图书管理",
          icon: 'fa-table',
          show:true,
          children: [
            {
              path: '/',
              redirect: 'booklist',
              show:false
            },
            {
              path: 'booklist',
              component: booklist,
              title: "图书列表",
              show:true
            },
            {
              path: 'bookAdd',
              component: bookAdd,
              title: "添加图书",
              show:true
            },
            {
              path: 'bookEdit',
              component: bookEdit,
              title: "编辑图书",
              show:false
            },
            {
              path: 'bookdetail',
              component: bookdetail,
              title: "图书详情",
              show:false
            }
          ]
        },
        {
          path: 'setting',
          component: setting,
          title: "设置",
          show:true,
          icon: 'fa-cog',
          children: [
            {
              path: '/',
              redirect: 'userInfo',
              show:false
            },
            {
              path: 'userInfo',
              component: userInfo,
              title: "修改个人信息",
              show:true
            },
            {
              path: 'password',
              component: password,
              title: "修改密码",
              show:true
            }
          ]
        }
      ]
   }
]



const router = new vueRouter({
  mode: 'history',
  routes,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if(to.name!='login'&&!sessionStorage.getItem('user')){
      next({path:'/login'});
  }
  if(to.title){
     document.title=to.meta.title;
  }
  store.dispatch('user/setUserInfo');
  next();
})

export default router;
